<?php

/**
 * 创建商户 appId,secret
 */
namespace App\Service\Merchant;

class CreateMerchant
{
    const TAG = 'mb';
    private static $instance;
    public static function instance()
    {
        $class_name = get_called_class();
        if (empty(self::$instance[$class_name])) {
            self::$instance[$class_name] = new static();
        }
        return self::$instance[$class_name];
    }
    
    /**
     * 创建appid
     * @param number $bid
     * @return string
     */
    public function createAppId( $bid = 0 ) {
        return self::TAG.sha1($bid.$this->createNoticeStr());
    }
    
    /**
     * 创建密钥
     * @param number $bid
     * @param string $appId
     * @return string
     */
    public function createSecret( $bid = 0 ,$appId = '') {
        $string = $bid .$appId .time();
        return sha1(md5($string));
    }
    
    //生成id   要防止生成纯数字
    private function createNoticeStr() {
        $time = substr(date("YmdHis", time()), 3);
        $randNum =$this->randString();
        if (is_numeric($randNum)) {
            //纯数字检测，如果是纯数字，重新调用该方法
            $b = $this->createNoticeStr();
            $uuid = $time . $b;
        } else {
            $uuid = $time . $randNum;
        }
        //因为生成的Id是15位的，所以取最后的15位
        return substr($uuid, -15);
    }
    
    //生成随机数  纯数字就再次调用
    private function randString() {
    
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        $randomString = '';
        for ($i = 0; $i < 4; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }
}